﻿using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
using OASystem.BLL.IService;
using OASystem.BLL.Service;
using OASystem.Common;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
/// <summary>
/// 通知及添加页简单操作
/// </summary>
namespace OASystem.Controllers
{
    public class AnnouncementController : Controller
    {
        public static Announcement user;
      
        /// <summary>
        /// 通知注入
        /// </summary>
        #region 通知注入
        private IAnnouncementService _AnnouncementService;
        private ILoginService _loginService;
        public AnnouncementController(IAnnouncementService AnnouncementService, ILoginService loginService)
        {
            _AnnouncementService = AnnouncementService;
            _loginService = loginService;
        }

        #endregion
        /// <summary>
        /// 单个通知发布
        /// </summary>
        /// <returns></returns>
        #region 单个通知发布
        public JsonResult AnnouncementByOne(Announcement announcement, int departId)
        {
            #region 单个通知
            try
            {
                ////实例化页面实体层对象
                ResponseResult responseResult = new ResponseResult();
                AnnouncementService announcementService = new AnnouncementService();
                responseResult = announcementService.AnnouncementByOne(announcement, departId);
                ////接收添加结果是否成功
                Announcement _announcement = new Announcement();
                _announcement = AnnouncementService.AresponseResult;
                //查询姓名相关的Id
                Expression<Func<Userinfo, bool>> lambdaExpression = a => a.UserName == _announcement.Publisher;
                List<Userinfo> userinfos = _loginService.QueryList(lambdaExpression);
                //singR通知
                IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<MyChatHub>();
                chat.Clients.User(userinfos[0].UserId.ToString()).add(_announcement.Ann_Title);



              





                //提交到前台处理
                return Json(responseResult);
            }
            catch (Exception)
            {

                throw;
            }
            #endregion
        }
        #endregion

        /// <summary>
        /// 多个通知发布
        /// </summary>
        /// <returns></returns>
        #region 多个通知发布
        public JsonResult AnnouncementByMore(Announcement announcement, int departId)
        {
            //对象id字符串类型集合
            List<string> userId = new List<string>();
            #region 多个通知
            try
            {
                #region 添加成功SingR提交
                ////实例化页面实体层对象
                ResponseResult responseResult = new ResponseResult();
                AnnouncementService announcementService = new AnnouncementService();
                responseResult = announcementService.AnnouncementByMore(announcement, departId);
                ////接收添加结果是否成功
                List<Announcement> _announcement = new List<Announcement>();
                _announcement = AnnouncementService.AresponseResults;
                for (int i = 0; i < _announcement.Count; i++)
                {
                    Announcement announcement1 = new Announcement();
                    announcement1.Publisher = _announcement[i].Publisher;
                    //查询姓名相关的Id
                    Expression<Func<Userinfo, bool>> lambdaExpression = a => a.UserName == announcement1.Publisher;
                    Userinfo userinfos = _loginService.Query(lambdaExpression);
                    userId.Add(userinfos.UserId.ToString());
                    //singR通知

                    #endregion

                }
                IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<MyChatHub>();
                chat.Clients.Users(userId).add(_announcement[0].Ann_Title);
                responseResult.success = true;
                // 提交到前台处理
                return Json(responseResult);
            }
            catch (Exception)
            {

                throw;
            }
            #endregion

        }
        #endregion
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        #region 分页查询
        public JsonResult NoticPageList(int page, int limit, string username)
        {
            //创建ResponseResult对象
            ResponseResult responseResult = new ResponseResult();
            //创建AnnouncementService对象
            AnnouncementService announcementService = new AnnouncementService();
            //调用分页
            responseResult.data = announcementService.NoticPageList(page, limit, username);
            //提交到前台处理
            return Json(responseResult.data, JsonRequestBehavior.AllowGet);
        }
        #endregion
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="announcement"></param>
        /// <returns></returns>
        #region 删除
        public JsonResult NoticDel(Announcement announcement)
        {
            //实例化页面实体层对象
            Operate operate = new Operate();
            //接收添加结果是否成功
            operate.Success = _AnnouncementService.Delete(announcement);
            //提交到前台处理
            return Json(operate);
        }

        #endregion

        public JsonResult DingDingApis()
        {          
              
            


            return Json(null);
       
         }
}
}